import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.net.URL;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class JButtonDemo extends JPanel implements ActionListener {
    protected static JButton cardButton1, cardButton2, cardButton3;

    protected Card carta1 = new Card(1);
    protected Card carta2 = new Card(2);
    protected Card carta3 = new Card(3);
    
    public JButtonDemo() {
    	
    	//Create Icons that can be used with the jButtons
//        ImageIcon leftButtonIcon = createImageIcon("rightarrow.JPG");
//        ImageIcon middleButtonIcon = createImageIcon("Pikachu Blender.PNG");
//        ImageIcon rightButtonIcon = createImageIcon("leftarrow.JPG");

        cardButton1 = new JButton(carta1.getIcon());
//        cardButton1.setVerticalTextPosition(AbstractButton.CENTER);
//        cardButton1.setHorizontalTextPosition(AbstractButton.LEADING); //left-to-right
//        cardButton1.setMnemonic(KeyEvent.VK_D);						   // Alt-D clicks the button
        cardButton1.setActionCommand("carta1");
//        cardButton1.setToolTipText("disable the Centre button.");		//Adding Tooltips
        cardButton1.setBorder(BorderFactory.createEmptyBorder());
        cardButton1.setContentAreaFilled(false);

        
        cardButton2 = new JButton(carta2.getIcon());
//        cardButton2.setVerticalTextPosition(AbstractButton.BOTTOM);
//        cardButton2.setHorizontalTextPosition(AbstractButton.CENTER);
//        cardButton2.setMnemonic(KeyEvent.VK_M);						// Alt-M clicks the button
//        cardButton2.setToolTipText("Centre button");
        cardButton2.setBorder(BorderFactory.createEmptyBorder());
        cardButton2.setContentAreaFilled(false);
        cardButton2.setActionCommand("carta2");


        cardButton3 = new JButton(carta3.getIcon());
        //Use the default text position of CENTER, TRAILING (RIGHT).
//        cardButton3.setMnemonic(KeyEvent.VK_E);						// Alt-E clicks the button
//        cardButton3.setActionCommand("carta3");
//        cardButton3.setEnabled(false);									// Disable the Button at creation time
        cardButton3.setBorder(BorderFactory.createEmptyBorder());
        cardButton3.setContentAreaFilled(false);
        cardButton3.setActionCommand("carta3");
        
        //Listen for actions on Left and Roght Buttons
        cardButton1.addActionListener(this);
        cardButton2.addActionListener(this);
        cardButton3.addActionListener(this);
//        cardButton3.setToolTipText("Enable the Centre button.");


        //Add Components to the frame, using the default FlowLayout.
        add(cardButton2);
        add(cardButton1);
        add(cardButton3);
    }

    public void actionPerformed(ActionEvent e) {
        if ("carta1".equals(e.getActionCommand())) {
            
        	System.out.println(carta1.getId());
            
        	if(carta1.getMark() == false){
	            cardButton1.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
	            carta1.setMark(true);
        	}else{
        		cardButton1.setBorder(BorderFactory.createEmptyBorder());
        		carta1.setMark(false);
        	}
        	
        }else if("carta2".equals(e.getActionCommand())){
        	
        	System.out.println(carta2.getId());
            
        	if(carta2.getMark() == false){
	        	cardButton2.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
	        	carta2.setMark(true);
        	}else {
        		cardButton2.setBorder(BorderFactory.createEmptyBorder());
        		carta2.setMark(false);
        	}
        	
        }else{
        	
        	System.out.println(carta3.getId());
            
        	if(carta3.getMark() == false){
	            cardButton3.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
	            carta3.setMark(true);
        	}else {
        		cardButton3.setBorder(BorderFactory.createEmptyBorder());
        		carta3.setMark(false);
        	}
        }
    }

    // Returns an ImageIcon, or null if the path was invalid. 
    protected static ImageIcon createImageIcon(String path) {
        URL imgURL = JButtonDemo.class.getResource(path);	//import java.net.URL; 
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find image in system: " + path);
            return null;
        }
    }

    //Create the GUI and show it.  
    private static void createGUI() {

        JFrame.setDefaultLookAndFeelDecorated(true);	        //Have nice window decorations.

        //Create and set up the frame.
        JFrame frame = new JFrame("jButton usage demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JButtonDemo buttonContentPane = new JButtonDemo();
        
        buttonContentPane.setOpaque(true); 						//content panes must be opaque
        frame.getRootPane().setDefaultButton(cardButton2);  
        
        frame.setContentPane(buttonContentPane); 
       
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createGUI(); 
            }
        });
    }
}
